//
//  MainTableViewController.swift
//  WeiBo_Private
//
//  Created by Yangperlove on 16/1/10.
//  Copyright © 2016年 Yangperlove. All rights reserved.
//

import UIKit

class MainTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "dismissComposeVControl", name: ComposeViewControl, object: nil)
        
        //将系统自带的tabBar换成自定义的MainTabBar
        let mainTabBar = MainTabBar()
        setValue(mainTabBar, forKey: "tabBar")
        
        mainTabBar.plusBtn.addTarget(self, action: "plusBtnDidClick", forControlEvents: .TouchUpInside)

        //为tabBar添加四个导航控制器
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    func dismissComposeVControl() {
        self.dismissViewControllerAnimated(true, completion: nil)
        let baseVControl = self.childViewControllers.first as! BaseNavController
        let homeVControl = baseVControl.childViewControllers.first as! HomeTableViewController
        homeVControl.tableView.reloadData()
    }
    
    //点击加号按钮事件
    func plusBtnDidClick() {
        let nav = UINavigationController(rootViewController: ComposeViewController())
        
        self.presentViewController(nav, animated: true, completion: nil)
    }
    
    
    //为tabBar控制器添加子控制器
    func addChildViewController(vc: UIViewController, title: String, imageName: String) {

        let nav = BaseNavController(rootViewController: vc)
        //设置tabBar文本属性
        
        //第一种方法
        /*
        vc.title = title
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()],forState: UIControlState.Selected)
        
        //设置tabBar图片属性
        vc.tabBarItem.image = UIImage(named: imageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        */
        
        //第二种方法
        self.tabBar.tintColor = UIColor.orangeColor()
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.title = title
        
        self.addChildViewController(nav)
        

        }
   
    
}
